//
//  ZYLMultiselectView.m
//  Multiselect
//
//  Created by zhuyuelong on 16/7/18.
//  Copyright © 2016年 zhuyuelong. All rights reserved.
//

#import "ZYLMultiselectView.h"
#import "HSFoodtypeModel.h"
#define XFUIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0F]
@implementation ZYLMultiselectView

-(void)setDiyBackgroundColor:(UIColor *)diyBackgroundColor{
    _diyBackgroundColor = diyBackgroundColor;
    self.backgroundColor = self.diyBackgroundColor;
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.layer.cornerRadius = 6.0f;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1];
        self.resultArr = [NSMutableArray array];
        self.dataArr = [NSMutableArray array];
        self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 50) style:UITableViewStylePlain];
        
        self.tableview.delegate = self;
        self.tableview.dataSource = self;
        self.tableview.showsVerticalScrollIndicator = NO;
        self.tableview.backgroundColor = [UIColor clearColor];
        [self addSubview:self.tableview];
        self.tableview.scrollEnabled = YES;
        [self.tableview registerClass:[ZYLMultiselectTableViewCell class] forCellReuseIdentifier:@"zylmuselect"];
    }
    
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, self.tableview.frame.size.height + 10, self.frame.size.width/3, self.frame.size.height - self.tableview.frame.size.height - 15)];
    
    [self addSubview:confirmBtn];
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    confirmBtn.layer.cornerRadius = 5;
    confirmBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    confirmBtn.layer.borderWidth = 1;
    [confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - self.frame.size.width/3 - 20, self.tableview.frame.size.height + 10, self.frame.size.width/3, confirmBtn.frame.size.height)];
    [self addSubview:cancelBtn];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    cancelBtn.layer.cornerRadius = 5;
    cancelBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    cancelBtn.layer.borderWidth = 1;
    [cancelBtn addTarget:self action:@selector(canceBtnClick) forControlEvents:UIControlEventTouchUpInside];
    return self;
}

-(void)confirmBtnClick{
    
    self.SelectBlock(self.resultArr,1);
    [self disMiss];
}

-(void)canceBtnClick{
    
    self.SelectBlock(nil,0);
    [self disMiss];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZYLMultiselectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zylmuselect"];
    cell.backgroundColor = [UIColor clearColor];
    HSFoodtypeModel *model = self.dataArr[indexPath.row];
    cell.lbName.text = model.name;
    cell.lbName.textColor = self.cellLabelColor?self.cellLabelColor:XFUIColorFromRGB(0x333333);
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    
    cell.tag = model.type_id;
    if (self.resultArr.count != 0) {
        for (int i = 0; i < self.resultArr.count; i ++) {
            int row = [self.resultArr[i] intValue];
//            if (indexPath.row == row) {
//                cell.selectImage.image = [UIImage imageNamed:@"checked"];
//                break;
//            }else{
//                cell.selectImage.image = [UIImage imageNamed:@"unchecked"];
//            }
            if (cell.tag == row) {
                cell.selectImage.image = [UIImage imageNamed:@"checked"];
                break;
            }else{
                cell.selectImage.image = [UIImage imageNamed:@"unchecked"];
            }
        }
    }else{
        cell.selectImage.image = [UIImage imageNamed:@"unchecked"];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYLMultiselectTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.row == 0) {
        return;
    }
    if (self.resultArr.count != 0) {
        LGFLog(@"已选择有11111111111111111111");
        
        for (int i = 0; i < self.resultArr.count; i ++) {
            int row = [self.resultArr[i] intValue];
            if (cell.tag == row) {//已选中
                [self.resultArr removeObjectAtIndex:i];
                break;
            }
//                if (cell.tag != row) {
//                    [self.resultArr addObject:[NSString stringWithFormat:@"%ld",(long)cell.tag]];
//                    break;
//                }
            
            if (i == self.resultArr.count - 1) {
                if (cell.tag != row) {
                    [self.resultArr addObject:[NSString stringWithFormat:@"%ld",(long)cell.tag]];
                }
                break;
            }
            
//            if (indexPath.row == row) {//已选
//                [self.resultArr removeObjectAtIndex:i];
//                break;
//            }
//            if (i == self.resultArr.count - 1) {
//                if (indexPath.row != row) {
//                    [self.resultArr addObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
//                }
//                break;
//            }
        }
    }else{
         LGFLog(@"未选择000000000000000000000");
        [self.resultArr addObject:[NSString stringWithFormat:@"%ld",(long)cell.tag]];
    }
    [self.tableview reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableview.frame.size.width, 50)];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, self.tableview.frame.size.width, 1)];
    lineView.backgroundColor = [UIColor whiteColor];
    [header addSubview:lineView];
    header.backgroundColor = self.backgroundColor;
    UILabel *lbTitle = [[UILabel alloc] initWithFrame:header.bounds];
    [header addSubview:lbTitle];
    if (self.heraderStr != nil) {
        lbTitle.text = self.heraderStr;
    }else{
        lbTitle.text = @"选择分类";
    }
    lbTitle.textColor = [UIColor whiteColor];
    lbTitle.textAlignment = NSTextAlignmentCenter;
    return header;
}

-(void)disMiss{
    
    [self removeFromSuperview];
}

@end
